using System.Data;
using System.Data.SqlClient;
using System.Text;
using Customs.Components.KDT.SXXK.Info;
using Microsoft.ApplicationBlocks.Data;

namespace Customs.Components.KDT.SXXK
{
	public class DinhMuc : DinhMucInfo
	{
		public void Load(DinhMucInfo entityInfo)
		{
			this._SoTiepNhan = entityInfo.SoTiepNhan;
			this._MaHaiQuan = entityInfo.MaHaiQuan;
			this._NamTiepNhan = entityInfo.NamTiepNhan;
			this._MaSanPham = entityInfo.MaSanPham;
			this._MaNguyenPhuLieu = entityInfo.MaNguyenPhuLieu;
			this._MaDoanhNghiep = entityInfo.MaDoanhNghiep;
			//this._DVT_ID = entityInfo.DVT_ID;
			this._DinhMucSuDung = entityInfo.DinhMucSuDung;
			this._TyLeHaoHut = entityInfo.TyLeHaoHut;
			this._GhiChu = entityInfo.GhiChu;
			this._SoThuTuHang = entityInfo.SoThuTuHang;
		}
		
		//---------------------------------------------------------------------------------------------
		public void InsertTransaction(SqlTransaction transaction, DinhMucInfoCollection collection)
		{
			DinhMuc item = new DinhMuc();
			foreach (DinhMucInfo itemInfo in collection)
			{
				item.Load(itemInfo);
				item.InsertTransaction(transaction);
			}						
		}
		
		public void InsertTransaction(SqlTransaction transaction)
		{
			StringBuilder query = new StringBuilder();
			query.Append("INSERT INTO DDINHMUCDK_D (");
			query.Append("So_TNDKDT, ");
			query.Append("MA_HQTN, ");
			query.Append("Nam_TN, ");
			query.Append("MA_SP, ");
			query.Append("MA_NPL, ");
			query.Append("Ma_DV, ");
            //query.Append("MA_DVT, ");
			query.Append("DM_SD, ");
			query.Append("TL_HH, ");
			query.Append("GHI_CHU, ");
			query.Append("STTHang ");
			query.Append(") ");

			query.Append("VALUES (");

			query.Append("@So_TNDKDT, ");
			query.Append("@MA_HQTN, ");
			query.Append("@Nam_TN, ");
			query.Append("@MA_SP, ");
			query.Append("@MA_NPL, ");
			query.Append("@Ma_DV, ");
            //query.Append("@MA_DVT, ");
			query.Append("@DM_SD, ");
			query.Append("@TL_HH, ");
			query.Append("@GHI_CHU, ");
			query.Append("@STTHang)");

			SqlParameter[] parameters = new SqlParameter[11];

			parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
			parameters[0].Direction = ParameterDirection.Input;
			parameters[0].Value = this._SoTiepNhan;

			parameters[1] = new SqlParameter("@MA_HQTN", SqlDbType.Char);
			parameters[1].Size = 6;
			parameters[1].Direction = ParameterDirection.Input;
			parameters[1].Value = this._MaHaiQuan;

			parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
			parameters[2].Direction = ParameterDirection.Input;
			parameters[2].Value = this._NamTiepNhan;

			parameters[3] = new SqlParameter("@MA_SP", SqlDbType.VarChar);
			parameters[3].Size = 30;
			parameters[3].Direction = ParameterDirection.Input;
			parameters[3].Value = this._MaSanPham;

			parameters[4] = new SqlParameter("@MA_NPL", SqlDbType.VarChar);
			parameters[4].Size = 30;
			parameters[4].Direction = ParameterDirection.Input;
			parameters[4].Value = this._MaNguyenPhuLieu;

			parameters[5] = new SqlParameter("@Ma_DV", SqlDbType.VarChar);
			parameters[5].Size = 14;
			parameters[5].Direction = ParameterDirection.Input;
			parameters[5].Value = this._MaDoanhNghiep;

            //parameters[6] = new SqlParameter("@MA_DVT", SqlDbType.Char);
            //parameters[6].Size = 3;
            //parameters[6].Direction = ParameterDirection.Input;
            //parameters[6].Value = this._DVT_ID;

			parameters[6] = new SqlParameter("@DM_SD", SqlDbType.Decimal);
			parameters[6].Precision = 18;
			parameters[6].Scale = 5;
			parameters[6].Direction = ParameterDirection.Input;
			parameters[6].Value = this._DinhMucSuDung;

			parameters[7] = new SqlParameter("@TL_HH", SqlDbType.Decimal);
			parameters[7].Precision = 18;
			parameters[7].Scale = 5;
			parameters[7].Direction = ParameterDirection.Input;
			parameters[7].Value = this._TyLeHaoHut;

			parameters[8] = new SqlParameter("@GHI_CHU", SqlDbType.VarChar);
			parameters[8].Size = 240;
			parameters[8].Direction = ParameterDirection.Input;
			parameters[8].Value = this._GhiChu;

			parameters[9] = new SqlParameter("@STTHang", SqlDbType.Int);
			parameters[9].Direction = ParameterDirection.Input;
			parameters[9].Value = this._SoThuTuHang;

			SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query.ToString(), parameters);
		}

       //-----------------------------------------------------------------------------------------
        public static void Delete(SqlTransaction transaction, long soTiepNhan, string maHaiQuan, string maDoanhNghiep, int namTiepNhan)
        {
            string query = string.Format("DELETE FROM DDINHMUCDK_D WHERE SO_TNDKDT = {0} AND MA_HQTN = '{1}' AND MA_DV = '{2}' AND NAM_TN = {3}", soTiepNhan, maHaiQuan.PadRight(6), maDoanhNghiep, namTiepNhan);
            SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query);
        }
	}
}